//
//  GTDetailViewController.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/14.
//

#import <WebKit/WebKit.h>
#import "../../Utils/GTMediator.h"
#import "../../Utils/GTScreen.h"
#import "GTDetailViewController.h"
@interface GTDetailViewController ()<WKNavigationDelegate>
@property (nonatomic, strong, readwrite) WKWebView *webview;
@property (nonatomic, strong, readwrite) UIProgressView *progressView;
@property (nonatomic, strong, readwrite) NSString *articleUrl;
@end

@implementation GTDetailViewController
+ (void)load {
//    [GTMediator registerScheme:@"detail://"
//                  processBlock:^(NSDictionary *_Nonnull params) {
//        NSString *url = [params objectForKey:@"url"];
//        UINavigationController *controller = (UINavigationController *)[params objectForKey:@"controller"];
//        GTDetailViewController *detailController = [[GTDetailViewController alloc] initWithUrlString:url];
//        detailController.title = [params objectForKey:@"title"];
//        detailController.view.backgroundColor = [UIColor whiteColor];
//        [controller pushViewController:detailController
//                              animated:YES];
//    }];
	[GTMediator registerProtocol:@protocol(GTDetailViewControlProtocol) cls:[self class]];
}

- (instancetype)initWithUrlString:(NSString *)urlString {
    self = [super init];

    if (self) {
        self.articleUrl = urlString;
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.navigationItem.backButtonTitle=@"返回";
    self.webview = [[WKWebView alloc]init];
    self.webview.navigationDelegate = self;
    self.webview.frame = CGRectMake(0, STATUSBARHEIGHT + 44, self.view.frame.size.width, self.view.frame.size.height - STATUSBARHEIGHT - 44);
    [self.view addSubview:self.webview];
    [self.view addSubview:({
        self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, STATUSBARHEIGHT + 44, self.view.frame.size.width, 20)];
        self.progressView;
    })];


    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.articleUrl]]];
    [self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)dealloc {
    [self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
    NSLog(@"");
}

- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context {
    NSLog(@"");
    self.progressView.progress = self.webview.estimatedProgress;
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    decisionHandler(WKNavigationActionPolicyAllow);

    NSLog(@"加载中");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    NSLog(@"加载完成");
    [self.progressView removeFromSuperview];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"加载失败");
}

#pragma mark - protocol
- (__kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl{
	return [[[self class]alloc]initWithUrlString:detailUrl];
}
/*
 #pragma mark - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
   }
 */

@end
